<?php
/*
 * ThinkSAAS APP入口
 * @copyright (c) 2010-3000 ThinkSAAS All Rights Reserved
 * @code by QiuJun
 * @Email:qiniao@vip.qq.com
 */
defined('IN_TS') or die('Access Denied.');

define('IS_POST', (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'));

//APP模板CSS,IMG,INC
$TS_APP['tpl']	= array(
	'skin'	=> 'app/'.$app.'/skins/',
	'js'	=> 'app/'.$app.'/js/',
);
 
 //system系统管理模板CSS,IMG
$TS_APP['system']	= array(
	'skin'	=> 'app/system/skins/',
	'js'	=> 'app/system/js/',
);

//定义全站公用CSS，IMG
$TS_PUB['css'] = 'data/public/css/';
$TS_PUB['img'] = 'data/public/image/';

//加载APP应用首页和配置文件
if(is_file('app/'.$app.'/action/'.$ac.'.php')){

	//加载系统缓存文件 
	$TS_SITE['base'] = AppCacheRead('system','options.php');
	$TS_SITE['base']['site_counter'] = stripslashes($TS_SITE['base']['site_counter']);
	
	//加载APP配置缓存文件
	if(is_dir('data/cache/'.$app) && $app != 'system'){
		if(is_file('data/cache/'.$app.'/options.php')){
			$TS_APP['options'] = AppCacheRead($app,'options.php');
			if($TS_APP['options']['isenable']=='1' && $ac != 'admin') qiMsg($app."应用关闭，请开启后访问！");
		}
	}

	//加载APP配置文件
	include_once 'app/'.$app.'/config.php';

	//连接数据库
	include_once 'thinksaas/mysql.php';
	$DB	= new MySql($TS_DB['host'],$TS_DB['user'],$TS_DB['pwd'],$TS_DB['name']);
	
	//加载APP数据库操作类并建立对象
	include_once 'app/'.$app.'/class.'.$app.'.php';
	$new[$app] = new $app($DB);
	
	//控制是否需要登录
	if($TS_USER['user'] == '' && $ac == 'set') header("Location: index.php?app=user&ac=login");

	//控制前台ADMIN访问权限
	if($ac == 'admin' && $TS_USER['user']['isadmin']!='1' && $app != 'system') header("Location: index.php?app=user&ac=login");
	
	//控制后台访问权限
	if($TS_USER['user']['isadmin'] != '1' && $app == 'system') header("Location: index.php?app=user&ac=login");
	
	//控制插件设置权限
	if($TS_USER['user']['isadmin'] != '1' && $in == 'edit') header("Location: index.php?app=user&ac=login");
	
	//判断用户是否上传头像
	if($TS_SITE['base']['isface']=='1' && $TS_USER['user'] != '' && $app != 'system' &&$ac != 'admin'){
		$userid = $TS_USER['user']['userid'];
		$faceUser = $DB->once_fetch_assoc("select * from ".dbprefix."app_user_info where userid='$userid'");
		if($faceUser['face']=='' && $ts != 'face') header("Location: index.php?app=user&ac=set&ts=face");
	}
	
	//运行统计结束
	$time_end = getmicrotime();
	
	$runTime = $time_end - $time_start;
	$runTime = number_format($runTime,6);
	
	//用户自动登录
	if($TS_USER['user']=='' && $_COOKIE['ts_email']!='' && $_COOKIE['ts_pwd'] !='' ){
		
		$loginUserNum = $DB->once_num_rows("select * from ".dbprefix."app_user where email='".$_COOKIE['ts_email']."' and pwd='".$_COOKIE['ts_pwd']."'");
	
		if($loginUserNum > 0){
			$loginUserData = $DB->once_fetch_assoc("select  * from ".dbprefix."app_user_info where email='".$_COOKIE['ts_email']."'");
			//用户session信息
			$_SESSION['tsuser']	= $loginUserData;
			$TS_USER = array(
				'user'		=> $_SESSION['tsuser'],
			);
			//更新登录时间
			$DB->query("update ".dbprefix."app_user_info set `uptime`='".time()."' where userid='".$loginUserData['userid']."'");	
		}
	}
	
	//加载公用插件 
	$tsHooks = array();
	if(is_dir('data/cache/pubs') && $app != 'pubs'){
		if(is_file('data/cache/pubs/plugins.php')){
		$public_plugins = AppCacheRead('pubs','plugins.php');
			if ($public_plugins && is_array($public_plugins)) {
				foreach($public_plugins as $plugin) {
					if(is_file('plugins/pubs/'.$plugin.'/'.$plugin.'.php')) {
						require_once 'plugins/pubs/'.$plugin.'/'.$plugin.'.php';
					}
				}
			}
		}
	}
	
	//加载APP插件
	if(is_dir('data/cache/'.$app) && $app != 'pubs'){
		if(is_file('data/cache/'.$app.'/plugins.php')){
			$active_plugins = AppCacheRead($app,'plugins.php');
			if ($active_plugins && is_array($active_plugins)) {
				foreach($active_plugins as $plugin) {
					if(is_file('plugins/'.$app.'/'.$plugin.'/'.$plugin.'.php')) {
						require_once 'plugins/'.$app.'/'.$plugin.'/'.$plugin.'.php';
					}
				}
			}
		}
	}
	
	//开始执行APP action
	include_once $app.'/action/'.$ac.'.php';
	
	
	
}else{
	header("HTTP/1.1 404 Not Found");
	qiMsg('sorry:no app!');
	exit;
}